#!/bin/bash -x

. $TONE_ROOT/lib/disk.sh
use_loop_dev_flag=0
kernel_ver=$(uname -r | awk -F '.' '{print$1"."$2}')
os_ver=$(uname -r | awk -F '.' '{print$(NF-1)}')
test_disk_list_org=()
test_disk_list_10G=()
test_disk_list=()

add_user()
{
    [ -n "$1" ] || return
    grep -q -w "$1" /etc/passwd && return
    useradd $1
}

select_require_hard_disk()
{
    disk_list_org=($(echo "$1"))
    # 磁盘容量下限，单位kb
    disk_size_min_kb=$2
    disk_list_new=()
    disk_num=${#disk_list_org[*]}
    for((index=0; index<$disk_num; index++));do
        dev=${disk_list_org[$index]}
        total_size=$(fdisk -l $dev |grep "Disk $dev:"|awk '{print $3}')
        unit=$(fdisk -l $dev |grep "Disk $dev:"|awk '{print $4}'|awk -F "," '{print $1}')
        if [ $unit == "KB" ] || [ $unit == "KiB" ];then
            total_size_kb=$total_size
        elif [ $unit == "MB" ] || [ $unit == "MiB" ];then
            total_size_kb=$(echo $total_size*1024|bc)
        elif [ $unit == "GB" ] || [ $unit == "GiB" ];then
            total_size_kb=$(echo $total_size*1024*1024| bc)
        elif [ $unit == "TB" ] || [ $unit == "TiB" ];then
            total_size_kb=$(echo $total_size*1024*1024*1024| bc)
        else
            exit 1
        fi
        # 筛选磁盘空间大于disk_size_min_kb的磁盘
        if [ $(echo "$total_size_kb>=$disk_size_min_kb"|bc) -eq 1 ];then
            disk_list_new[${#disk_list_new[*]}]=$dev
        fi
    done
    echo ${disk_list_new[*]}
}

prepare_disk_for_test()
{
    nr_disk=4
    if [[ "$fs" == ext4 ]]; then
        export mkfsopt=" -q -F"
    elif [ $fs  == xfs ]; then
        export mkfsopt=" -f "
    else
        export mkfsopt=""
    fi

    if [ -n "$TEST_DISK" ]; then
        echo "TEST_DISK is:"$TEST_DISK
        test_disk_list_org=($(echo $TEST_DISK|tr ',' ' '))
    else
        test_disk_list_org=($(eval get_test_disks))
    fi
    echo "test disk list org is:"${test_disk_list_org[@]}

    disk_num=${#test_disk_list_org[*]}
    if [ $disk_num -gt 0 ]; then
        # 磁盘容量下限10G，单位KB
        disk_size_min_kb=10485760
        # 筛选磁盘容量大于10G的磁盘
        test_disk_list_10G=($(select_require_hard_disk "${test_disk_list_org[*]}" $disk_size_min_kb))
        echo "disk space greate than 10G is:"${test_disk_list_10G[@]}
        disk_num=${#test_disk_list_10G[*]}
        # 可用磁盘小于4块时，需要将一块盘分四个区，此时需要删除磁盘空间小于40G的磁盘
        if [ $disk_num -gt 0 ] && [ $disk_num -lt $nr_disk ]; then 
            # 磁盘容量下限40G，单位KB
            disk_size_min_kb=41943040
            # 筛选磁盘容量大于40G的磁盘
            test_disk_list=($(select_require_hard_disk "${test_disk_list_10G[*]}" $disk_size_min_kb))
            echo "disk space greate than 40G is:"${test_disk_list[@]}
        else
            test_disk_list=("${test_disk_list_10G[@]}")
        fi
    fi
    echo "test disk list:"${test_disk_list[@]}

    disk_num=${#test_disk_list[*]}
    if [ $disk_num -gt 0 ] && [ $disk_num -lt $nr_disk ]; then
        setup_disk_fs ${test_disk_list[0]} "$nr_disk" "mounted"
        disk_list=($(eval get_dev_partition))
        mounted_list=($(eval get_mount_points))
        umount_fs
        export FSTYP=$fs
        export TEST_DEV="${disk_list[0]}"
        export SCRATCH_DEV="${disk_list[1]}"
        export TEST_LOGDEV="${disk_list[2]}"
        export SCRATCH_LOGDEV="${disk_list[3]}"
        export TEST_DIR=${mounted_list[0]}
        export SCRATCH_MNT=${mounted_list[1]}
    elif [ $disk_num -ge $nr_disk ]; then
        mkfs.$fs $mkfsopt ${test_disk_list[0]}
        mkfs.$fs $mkfsopt ${test_disk_list[1]}
        mkfs.$fs $mkfsopt ${test_disk_list[2]}
        mkfs.$fs $mkfsopt ${test_disk_list[3]}
        mkdir -p /mnt/test /mnt/scratch
        export FSTYP=$fs
        export TEST_DEV="${test_disk_list[0]}"
        export SCRATCH_DEV="${test_disk_list[1]}"
        export TEST_LOGDEV="${test_disk_list[2]}"
        export SCRATCH_LOGDEV="${test_disk_list[3]}"
        export TEST_DIR=/mnt/test
        export SCRATCH_MNT=/mnt/scratch
    else
        # 无空闲磁盘时判断根分区剩余空间，小于30G报错，退出
        need_disk_size=30720 #单位M，30G
        disk_free_size=$(df -m|grep -w "/"|awk '{print $4}')
        if [ $disk_free_size -lt $need_disk_size ]; then
            echo "The system has no free disk, so using the loop device to test requires at least 30720M(30G) of space. The current system disk has only ${disk_free_size}M of space, so the test cannot be performed."
            exit 1
        fi
        use_loop_dev_flag=1
        for index in $(seq 90 93); do
            [ -e /dev/loop$index ] && continue
            mknod -m 0660 /dev/loop$index b 7 $index
            chown root:disk /dev/loop$index
        done
        dd if=/dev/zero of=/tmp/loop90.img bs=1G count=10
        dd if=/dev/zero of=/tmp/loop91.img bs=1G count=10
        dd if=/dev/zero of=/tmp/loop92.img bs=1G count=5
        dd if=/dev/zero of=/tmp/loop93.img bs=1G count=5

        for index in $(seq 90 93); do
            mkfs.$fs $mkfsopt /tmp/loop${index}.img
            losetup /dev/loop${index} /tmp/loop${index}.img
        done

        mkdir -p /mnt/test /mnt/scratch
        export FSTYP=$fs
        export TEST_DEV=/dev/loop90
        export SCRATCH_DEV=/dev/loop91
        export TEST_LOGDEV=/dev/loop92
        export SCRATCH_LOGDEV=/dev/loop93
        export TEST_DIR=/mnt/test
        export SCRATCH_MNT=/mnt/scratch
    fi
    echo "test conf:"
    echo "FSTYP is: " $FSTYP
    echo "TEST_DEV is: " $TEST_DEV
    echo "SCRATCH_DEV is: " $SCRATCH_DEV
    echo "TEST_LOGDEV is: " $TEST_LOGDEV
    echo "SCRATCH_LOGDEV is: " $SCRATCH_LOGDEV
    echo "TEST_DIR is: " $TEST_DIR
    echo "SCRATCH_MNT is: " $SCRATCH_MNT
}

setup()
{
    add_user "fsgqa"
    add_user "123456-fsgqa"
    add_user "fsgqa2"
    prepare_disk_for_test
}

run()
{
    local opts=""
    local kver=$(uname -r | cut -d. -f1-2)
    local kex=$(uname -r | awk -F "." '{print $(NF-1)}')
    [ -d "blacklists" ] || mkdir blacklists
    [ -f "blacklists/exclude" ] && rm -f blacklists/exclude
    [ -f $TONE_BM_SUITE_DIR/blacklists/${fs}-exclude ] && \
        cat $TONE_BM_SUITE_DIR/blacklists/${fs}-exclude > blacklists/exclude
    [ -f $TONE_BM_SUITE_DIR/blacklists/${fs}-exclude-${kver} ] && \
        cat $TONE_BM_SUITE_DIR/blacklists/${fs}-exclude-${kver} >> blacklists/exclude
    [ -f $TONE_BM_SUITE_DIR/blacklists/${fs}-exclude-${kver}-${kex} ] && \
        cat $TONE_BM_SUITE_DIR/blacklists/${fs}-exclude-${kver}-${kex} >> blacklists/exclude
    [ -f "blacklists/exclude" ] && opts="$opts -E blacklists/exclude"
    ./check $opts -g "auto"

    return 0
}

parse()
{
    $TONE_BM_SUITE_DIR/parse.awk
}

teardown()
{
    if [ $use_loop_dev_flag -eq 1 ]; then
        losetup -d /dev/loop90
        losetup -d /dev/loop91
        losetup -d /dev/loop92
        losetup -d /dev/loop93
        rm -rf /tmp/loop90.img /tmp/loop91.img /tmp/loop92.img /tmp/loop93.img
        rm -rf /dev/loop90 /dev/loop91 /dev/loop92 /dev/loop93
    fi
}
